package com.xnx3.wangmarket.shop.core.service.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.xnx3.CacheUtil;
import com.xnx3.banmahuishou.userApplet.vo.CartVO;
import com.xnx3.j2ee.dao.SqlDAO;
import com.xnx3.j2ee.util.SessionUtil;
import com.xnx3.wangmarket.shop.core.service.CartService;

@Service
public class CartServiceImpl implements CartService {
	@Resource
	private SqlDAO sqlDAO;
	
	//某个用户缓存中购物车，这里是缓存的key，其中 {userid} 是当前登录用户的userid
	public static final String CURRENT_USER_CACHE_CART = "banmahuishou:cart:{userid}";

	public SqlDAO getSqlDAO() {
		return sqlDAO;
	}

	public void setSqlDAO(SqlDAO sqlDAO) {
		this.sqlDAO = sqlDAO;
	}

	@Override
	public CartVO getCurrentUserCacheCartVO() {
		int userid = SessionUtil.getUserId();
		CartVO vo = (CartVO) CacheUtil.get(CURRENT_USER_CACHE_CART.replace("{userid}", userid+""));
		if(vo == null) {
			vo = new CartVO();
		}
		
		return vo;
	}

	@Override
	public void setCurrentUserCacheCartVO(CartVO vo) {
		int userid = SessionUtil.getUserId();
		CacheUtil.set(CURRENT_USER_CACHE_CART.replace("{userid}", userid+""), vo);
	}

}
